﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.Collections;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.Common;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using System.Drawing;
using Org.Eurekaa.DNJ.Settings;
using Org.Eurekaa.DNJ.Common;

namespace Org.Eurekaa.DNJ.UI.WebControls
{

    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJSlider.bmp")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.JQueryUIControlDesigner))]
    [DefaultProperty("Settings")]
    public class DNJSlider : JQueryUIControl
    {
        //private Panel panel = new Panel();        
        private HtmlGenericControl slider = new HtmlGenericControl("div");
        private TextBox sliderValue = new TextBox();
        private TextBox sliderValue0 = new TextBox();
        private TextBox sliderValue1 = new TextBox();
        private String clientChangeFunction = "";

        private DNJSliderSettings _settings = new DNJSliderSettings();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJSliderSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJSliderEvents _events = new DNJSliderEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty), 
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJSliderEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }


        [Browsable(false)]
        public int Value
        {
            get { return _settings.Value; }
        }
        [Browsable(false)]
        public int MinValue
        {
            get { return _settings.Range ? _settings.Values.Min : _settings.Value; }
        }
        [Browsable(false)]
        public int MaxValue
        {
            get { return _settings.Range ? _settings.Values.Max : _settings.Value; }
        }



        public override void SaveSettings(ref object stateObject)
        {
            int val = 0;
            if (int.TryParse(sliderValue.Text, out val)) Settings.Value = val;
            if (int.TryParse(sliderValue0.Text, out val)) Settings.Values.Min = val;
            if (int.TryParse(sliderValue1.Text, out val)) Settings.Values.Max = val;

            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;
            
            stateObject = lstObj;
            GenericSettingsObject allSettings = new GenericSettingsObject();

            
            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            if (!string.IsNullOrEmpty(ClientEvents.Change.Name)) //special case : change event is internally used
            {
                ClientEvents.Change.script = ClientEvents.Change.Name + "(event, ui);";
                ClientEvents.Change.Name = "";
            }
            ClientEvents.Change.script = clientChangeFunction + ClientEvents.Change.script;
            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);



            PageHelper.AppendJQueryPlugin("#" + slider.ClientID, "slider", allSettings.ToString(), this.Page.Header);
        
        }


        public override void LoadSettings(object stateObject)
        {
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJSliderSettings);
            ClientEvents = (lstObj[1] as DNJSliderEvents);
        }


        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                string hw = Settings.Orientation == EnmOrientation.vertical ? "height:" + this.Height.ToString() : "width: " + this.Width.ToString();
                string trbl = Settings.Orientation == EnmOrientation.vertical ? "bottom" : "left";
                string orientation = Settings.Orientation == EnmOrientation.vertical ? "vertical" : "horizontal";

                writer.Write("<div style=\"padding:5px;display: inline-block;" + hw + ";\" >");
                writer.Write("<div class=\"ui-slider ui-slider-" + orientation + " ui-widget ui-widget-content ui-corner-all\" style=\"" + hw + "\"><a href=\"#\" class=\"ui-slider-handle ui-state-default ui-corner-all\" style=\"" + trbl + ": 40%;\">&nbsp;</a></div>");
                writer.Write("</div>");
            }
            else
                base.Render(writer);
        }

        protected override void CreateChildControls()
        {
            slider.ID = "div_" + this.ID;

            string hw = Settings.Orientation == EnmOrientation.vertical ? "height:" + this.Height.ToString() : "width: " + this.Width.ToString();
            slider.Attributes.Add("style", hw);            

            clientChangeFunction = "";
            if (Settings.Range)
            {
                sliderValue0.ID = "sliderValue1";
                sliderValue1.ID = "sliderValue2";
                sliderValue0.Style.Add("display", "none");
                sliderValue1.Style.Add("display", "none");
                Controls.Add(sliderValue0);
                Controls.Add(sliderValue1);
                clientChangeFunction = "$('#" + sliderValue0.ClientID + "').val($(this).slider('values', 0))";
                clientChangeFunction += ";$('#" + sliderValue1.ClientID + "').val($(this).slider('values', 1));";
            }
            else
            {
                sliderValue.ID = "sliderValue1";
                sliderValue.Style.Add("display", "none");
                Controls.Add(sliderValue);
                clientChangeFunction = "$('#" + sliderValue.ClientID + "').val(ui.value);";
            }



            Controls.Add(slider);

            base.CreateChildControls();

        }
    }


}
